package www.game;

import java.util.Arrays;
import java.util.Optional;

public class SpinWords {

    public String spinWords(String sentence) {
        //TODO: Code stuff here
        String[] split = sentence.split(" ");
        Optional<String> res = Arrays.stream(split).map(item -> {
            if (item.length() >= 5) {
                return reverse(item);
            }
            return item;
        }).reduce((left, right) -> {
            return left + " " + right;
        });
        return res.get();
    }

    private String reverse(String item) {
        StringBuilder res = item.chars().collect(StringBuilder::new,
            (str, num) -> {
                str.insert(0, (char) num);
            }, (str1, str2) -> {
                str1.insert(0, str2.toString());
            });
        return res.toString();
    }

}